<?php
// +----------------------------------------------------------------------
// | [RhaPHP System] Copyright (c) 2017 http://www.rhaphp.com/
// +----------------------------------------------------------------------
// | [RhaPHP] 并不是自由软件,你可免费使用,未经许可不能去掉RhaPHP相关版权
// +----------------------------------------------------------------------
// | Author: Geeson <qimengkeji@vip.qq.com>
// +----------------------------------------------------------------------

namespace addons\weiweb\controller;


use app\common\controller\Addon;
use think\Db;
use think\facade\Request;


class Webs extends Addon
{
    public $adminLogin = true;

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }
    public function index()
    {
        $type = input('type')==1 || input('type')==2 ? input('type') : 1;
        $this->assign('type', $type);
        if($type==1){
            if (Request::isAjax()) {
                $_data = input();

                if(!$_data['name']) {
                    ajaxMsg(0, '请输入网站名');
                }

                $web = Db::name('we_index')->where(['mid' => $this->mid])->find();
                $data = array(
                    'name'=>$_data['name']
                );
                if($web){
                    Db::name('we_index')->where(['mid' => $this->mid])->update($data);
                }else{
                    $data['mid'] = $this->mid;
                    Db::name('we_index')->insert($data);
                }
                ajaxMsg(1, '操作成功');

            } else {
                $web = Db::name('we_index')->where(['mid' => $this->mid])->find();
                $web['url'] = getHostDomain() . \addonUrl('/index/index',['mid'=>$this->mid]);
                if(!isset($web['name'])){
                    $web['name'] = '';
                }

                $this->assign('form', $web);
                $this->fetch();
            }
        }else if($type == 2){
            $slider_list = Db::name('we_slider')->where(['mid' => $this->mid,'columns_id' => 0])->order('sort ASC')->select();
            $this->assign('slider_list', $slider_list);
            $this->fetch();
        }
    }


    public function templet()
    {
        $type = input('type')==1 || input('type')==2 || input('type')==3 ? input('type') : 1;
        $this->assign('type', $type);
        $templetList = $this->getAllTemplet();

        if (Request::isAjax()) {
            $_data = input();
            $code = $_data['code'];
            if(!isset($templetList[$type][$code])){
                ajaxMsg(0, '模板不存在');
            }
            if( Db::name('we_templet')->where(['type' => $type,'mid'=>$this->mid])->find()){
                Db::name('we_templet')->where(['type' => $type,'mid'=>$this->mid])->update(['code' => $code]);
            }else{
                $data = array(
                    'code'=>$code,
                    'mid'=>$this->mid,
                    'type' => $type
                );
                Db::name('we_templet')->insert($data);
            }
            ajaxMsg(1, '操作成功');

        } else {
            $configList = Db::name('we_templet')->where(['mid'=>$this->mid])->select();
            $new_config = array();
            foreach ($configList as $item) {
                $new_config[$item['type']][$item['code']] = 1;
            }

            $configs = isset($templetList[$type]) ? $templetList[$type] : array();
            foreach ($configs as $k=>$item) {
                $configs[$k]['checked'] = 0;
                if(isset($new_config[$type][$item['code']])){
                    $configs[$k]['checked'] = 1;
                }
            }

            $this->assign('configList', $configs);

            $this->fetch();
        }
    }




    public function addSlider(){
        if (Request::isAjax()) {
            $_data = input();

            if(!$_data['describe']) {
                ajaxMsg(0, '请输入描述');
            }
            if(!$_data['img_path']) {
                ajaxMsg(0, '请上传图片');
            }

                $_data['sort'] = $_data['sort'] ? intval($_data['sort']) : 0;
                $_data['show'] = isset($_data['show']) && $_data['show'] == 'on' ?  1 : 0;
                $data = array(
                    'describe'=>$_data['describe'],
                    'url'=>$_data['url'],
                    'img_path'=>$_data['img_path'],
                    'mid'=>$this->mid,
                    'sort'=>$_data['sort'],
                    'show'=>$_data['show'],
                );
                if (Db::name('we_slider')->insert($data)) {
                    ajaxMsg(1, '操作成功');
                } else {
                    ajaxMsg(0, '操作失败');
                }

        } else {

            $this->fetch();
        }
    }
    public function addColumnsSlider(){
        if (Request::isAjax()) {
            $_data = input();

            if(!$_data['describe']) {
                ajaxMsg(0, '请输入描述');
            }
            if(!$_data['img_path']) {
                ajaxMsg(0, '请上传图片');
            }

            $_data['sort'] = $_data['sort'] ? intval($_data['sort']) : 0;
            $_data['show'] = isset($_data['show']) && $_data['show'] == 'on' ?  1 : 0;
            $data = array(
                'describe'=>$_data['describe'],
                'url'=>$_data['url'],
                'img_path'=>$_data['img_path'],
                'mid'=>$this->mid,
                'columns_id'=>$_data['columns'],
                'sort'=>$_data['sort'],
                'show'=>$_data['show'],
            );
            if (Db::name('we_slider')->insert($data)) {
                ajaxMsg(1, '操作成功');
            } else {
                ajaxMsg(0, '操作失败');
            }

        } else {
            $columns_list = Db::name('we_columns')->where(['mid' => $this->mid])->field('name,id')->select();

            $this->assign('columns_list', $columns_list);
            $this->fetch();
        }
    }

    public function addColumns(){
        if (Request::isAjax()) {
            $_data = input();

            if(!$_data['name']) {
                ajaxMsg(0, '请输入名称');
            }
            if(!$_data['img_path']) {
                ajaxMsg(0, '请上传图片');
            }

            $_data['sort'] = $_data['sort'] ? intval($_data['sort']) : 0;
            $_data['show'] = isset($_data['show']) && $_data['show'] == 'on' ?  1 : 0;
            $data = array(
                'name'=>$_data['name'],
                'img_path'=>$_data['img_path'],
                'mid'=>$this->mid,
                'sort'=>$_data['sort'],
                'show'=>$_data['show'],
            );
            if (Db::name('we_columns')->insert($data)) {
                ajaxMsg(1, '操作成功');
            } else {
                ajaxMsg(0, '操作失败');
            }

        } else {

            $this->fetch();
        }
    }

    public function editColumns(){
        $id = input('id');
        if (Request::isAjax()) {
            $_data = input();

            if(!$_data['name']) {
                ajaxMsg(0, '请输入名称');
            }
            if(!$_data['img_path']) {
                ajaxMsg(0, '请上传图片');
            }

            $_data['sort'] = $_data['sort'] ? intval($_data['sort']) : 0;
            $_data['show'] = isset($_data['show']) && $_data['show'] == 'on' ?  1 : 0;

            $data = array(
                'name'=>$_data['name'],
                'img_path'=>$_data['img_path'],
                'sort'=>$_data['sort'],
                'show'=>$_data['show'],
            );
            Db::name('we_columns')->where(['id'=>$id])->update($data);
            ajaxMsg(1, '操作成功');

        } else {
            $web = Db::name('we_columns')->where(['id' => $id])->find();
            $columns_list = Db::name('we_columns')->where(['mid' => $this->mid])->field('name,id')->select();


            $this->assign('columns_list', $columns_list);
            $this->assign('form', $web);
            $this->fetch();
        }
    }

    public function editArticle(){
        $id = input('id');
        if (Request::isAjax()) {
            $_data = input();

            if(!$_data['title']) {
                ajaxMsg(0, '请输入标题');
            }
            if(!$_data['cover']) {
                ajaxMsg(0, '请上传封面');
            }

            $_data['top'] = isset($_data['top']) && $_data['top'] == 'on' ?  1 : 0;
            $_data['show'] = isset($_data['show']) && $_data['show'] == 'on' ?  1 : 0;
            $data = array(
                'cover'=>$_data['cover'],
                'author'=>$_data['author'],
                'title'=>$_data['title'],
                'content_source_url'=>$_data['content_source_url'],
                'digest'=>$_data['digest'],
                'content'=>$_data['content'],
                'show'=>$_data['show'],
                'top'=>$_data['top'],
                'update_time'=>time(),
            );

            Db::name('we_article')->where(['id'=>$id])->update($data);
            ajaxMsg(1, '操作成功');

        } else {

            $this->assign('uploadImg', getHostDomain() . \url('mp/upload/uploadAddonsImg'));
            $web = Db::name('we_article')->where(['id' => $id])->find();
            $this->assign('form', $web);
            $this->fetch();
        }
    }

    public function editSlider(){
        $id = input('id');
        if (Request::isAjax()) {
            $_data = input();

            if(!$_data['describe']) {
                ajaxMsg(0, '请输入描述');
            }
            if(!$_data['img_path']) {
                ajaxMsg(0, '请上传图片');
            }

            $_data['sort'] = $_data['sort'] ? intval($_data['sort']) : 0;
            $_data['show'] = isset($_data['show']) && $_data['show'] == 'on' ?  1 : 0;
            $data = array(
                'describe'=>$_data['describe'],
                'url'=>$_data['url'],
                'img_path'=>$_data['img_path'],
                'sort'=>$_data['sort'],
                'show'=>$_data['show'],
            );
            Db::name('we_slider')->where(['id'=>$id])->update($data);
                ajaxMsg(1, '操作成功');

        } else {
            $web = Db::name('we_slider')->where(['id' => $id])->find();
            $this->assign('form', $web);
            $this->fetch();
        }
    }
    public function editColumnsSlider(){
        $id = input('id');
        if (Request::isAjax()) {
            $_data = input();

            if(!$_data['describe']) {
                ajaxMsg(0, '请输入描述');
            }
            if(!$_data['img_path']) {
                ajaxMsg(0, '请上传图片');
            }

            $_data['sort'] = $_data['sort'] ? intval($_data['sort']) : 0;
            $_data['show'] = isset($_data['show']) && $_data['show'] == 'on' ?  1 : 0;
            $data = array(
                'describe'=>$_data['describe'],
                'url'=>$_data['url'],
                'img_path'=>$_data['img_path'],
                'sort'=>$_data['sort'],
                'show'=>$_data['show'],
            );
            Db::name('we_slider')->where(['id'=>$id])->update($data);
            ajaxMsg(1, '操作成功');

        } else {
            $columns_list = Db::name('we_columns')->where(['mid' => $this->mid])->field('name,id')->select();

            $this->assign('columns_list', $columns_list);
            $web = Db::name('we_slider')->where(['id' => $id])->find();
            $this->assign('form', $web);
            $this->fetch();
        }
    }

    public function columns(){
        $type = input('type')==1 || input('type')==2 ? input('type') : 1;
        $this->assign('type', $type);
        if($type==1) {
            $columns_list = Db::name('we_columns')->where(['mid' => $this->mid])->order('sort ASC')->select();
            $this->assign('columns_list', $columns_list);
            $this->fetch();
        }
        else if($type == 2){
            $slider_list = Db::name('we_slider')->where('columns_id',"<>", 0)->where(['mid' => $this->mid])->order('columns_id DESC,sort ASC')->select();
            $columns = array();
            $columns_list = Db::name('we_columns')->where(['mid' => $this->mid])->order('sort ASC')->select();
            foreach ($columns_list as $k => $item) {
                $columns[$item['id']] = $item['name'];
            }
            foreach ($slider_list as $k=>$item) {
                $slider_list[$k]['columns_name'] = isset($columns[$item['columns_id']]) ? $columns[$item['columns_id']] : '';
            }
            $this->assign('slider_list', $slider_list);
            $this->fetch();
        }
    }

    public function article(){
        $article_list = Db::name('we_article')->where(['mid' => $this->mid])->order('top DESC,add_time DESC')->paginate(20);
        $columns_list = Db::name('we_columns')->where(['mid' => $this->mid])->field('name,id')->select();
        $columns = array();
        foreach ($columns_list as $k=>$item) {
            $columns[$item['id']] = $item['name'];
        }
        $data = [];
        foreach ($article_list as $key => $val) {
            $val['columns_name'] = '';
            if(isset($columns[$val['columns_id']])){
                $val['columns_name'] = $columns[$val['columns_id']];
            }
            $val['url'] = getHostDomain() . \addonUrl('/index/article',['mid'=>$this->mid,'id'=>$val['id']]);
            $data[$key] = $val;
        }
        $page = $article_list->render();
        $this->assign('page', $page);
        $this->assign('article_list', $data);
        $this->fetch();


    }

    /*
    * 从永久素材添加
    */
    public function addFromMaterial(){
        if (Request::isAjax()) {
            $_data = input();
            if(!isset($_data['columns'])){
                ajaxMsg('0', '请选择栏目');
            }
            $matids = $_data['matids'];
            $mat_list = Db::name('material_news')->where('id', 'in', $matids)->order('create_time DESC')->select();
            if(!$mat_list){
                ajaxMsg('0', '素材不存在');
            }

            foreach ($mat_list as $mat) {
                $data = array(
                    'cover'=>$mat['thumb_url'],
                    'author'=>$mat['author'],
                    'mid'=>$this->mid,
                    'content'=>$mat['content'],
                    'columns_id'=>$_data['columns'],
                    'title'=>$mat['title'],
                    'content_source_url'=>$mat['content_source_url'],
                    'digest'=>$mat['digest'],
                    'update_time'=>time(),
                    'add_time'=>time(),
                );
                Db::name('we_article')->insert($data);
            }
            ajaxMsg('0', '操作成功');
        }else {
            $mat_list = array();
            if($admin = session('admin')){
                $role_info =  Db::name('admin_role')->where('id',$admin['role'])->find();
                $mp_power = json_decode($role_info['mp_power'],true);
                $role = array_values($mp_power);
                $mat_list = Db::name('material_news')->alias('mn')->join('material ma','ma.id = mn.news_id')
                    ->order('mn.create_time DESC')->field('mn.*,ma.from_type')->where('mn.mpid','in', $role)->paginate(10);
            }
            if(!$mat_list){
                $mat_list = Db::name('material_news')->alias('mn')->join('material ma','ma.id = mn.news_id')
                    ->order('mn.create_time DESC')->field('mn.*,ma.from_type')->where('mn.mpid',$this->mid)->paginate(10);
            }

            $mplist = Db::name('mp')->select();
            $newmplist = [];
            foreach ($mplist as $item) {
                $newmplist[$item['id']] = $item['name'];
            }
            
            $columns_list = Db::name('we_columns')->where(['mid' => $this->mid])->field('name,id')->select();

            $columns = array();
            foreach ($columns_list as $k => $item) {
                $columns[$item['id']] = $item['name'];
            }
            $data = [];
            foreach ($mat_list as $key => $val) {
               if($val['from_type'] == 1){
                   $val['thumb_url'] = getHostDomain() . url('mp/Show/image','','').'?url='.urlencode($val['thumb_url']);
               }
                $val['mpname'] = isset($newmplist[$val['mpid']]) ? $newmplist[$val['mpid']] : '其他';
                $data[$key] = $val;
            }
            $page = $mat_list->render();
            $this->assign('page', $page);
            $this->assign('data_list', $data);
            $this->assign('columns_list', $columns_list);
            $this->fetch();
        }

    }

    /*
     * 从临时素材添加
     */
    public function addFromMaterial1(){
        if (Request::isAjax()) {
            $_data = input();
            if(!isset($_data['columns'])){
                ajaxMsg('0', '请选择栏目');
            }
            $matids = $_data['matids'];
            $mat_list = Db::name('media_news_list')->where('id', 'in', $matids)->order('id DESC')->select();
            if(!$mat_list){
                ajaxMsg('0', '素材不存在');
            }

            foreach ($mat_list as $mat) {
                $data = array(
                    'cover'=>$mat['cover'],
                    'author'=>$mat['author'],
                    'mid'=>$this->mid,
                    'content'=>$mat['content'],
                    'columns_id'=>$_data['columns'],
                    'title'=>$mat['title'],
                    'content_source_url'=>$mat['content_source_url'],
                    'digest'=>$mat['digest'],
                    'update_time'=>time(),
                    'add_time'=>time(),
                );
                Db::name('we_article')->insert($data);
            }
            ajaxMsg('0', '操作成功');
        }else {
            $mat_list = Db::name('media_news_list')->where('title', '<>', '')->order('id DESC')->paginate(10);
            $columns_list = Db::name('we_columns')->where(['mid' => $this->mid])->field('name,id')->select();

            $columns = array();
            foreach ($columns_list as $k => $item) {
                $columns[$item['id']] = $item['name'];
            }
            $data = [];
            foreach ($mat_list as $key => $val) {
                $data[$key] = $val;
            }
            $page = $mat_list->render();
            $this->assign('page', $page);
            $this->assign('data_list', $data);
            $this->assign('columns_list', $columns_list);
            $this->fetch();
        }
    }




    public function delSlider()
    {
        if (Request::isAjax()) {
            $result=Db::name('we_slider')->where(['mid' => $this->mid, 'id' => input('fid')])->find();
            if (!empty($result)){

                Db::name('we_slider')->where(['mid' => $this->mid, 'id' => input('fid')])->delete();

                ajaxMsg(1, '删除成功');
            }else{
                ajaxMsg(0, '没有删除的内容');
            }


        }
    }

    public function showSlider()
    {
        if (Request::isAjax()) {
            Db::name('we_slider')->where(['mid' => $this->mid, 'id' => input('fid')])->update(['show'=>input('show')]);

        }
    }

    public function delColumns()
    {
        if (Request::isAjax()) {
            $result=Db::name('we_columns')->where(['mid' => $this->mid, 'id' => input('fid')])->find();
            if (!empty($result)){
                $result=Db::name('we_article')->where(['mid' => $this->mid, 'columns_id' =>$result['id']])->find();
                if($result){
                    ajaxMsg(0, '请先删除栏目文章');
                }
                Db::name('we_slider')->where(['mid' => $this->mid, 'columns_id' => input('fid')])->delete();
                Db::name('we_columns')->where(['mid' => $this->mid, 'id' =>input('fid')])->delete();

                ajaxMsg(1, '删除成功');
            }else{
                ajaxMsg(0, '没有删除的内容');
            }


        }
    }

    public function showColumns()
    {
        if (Request::isAjax()) {
            Db::name('we_columns')->where(['mid' => $this->mid, 'id' => input('fid')])->update(['show'=>input('show')]);

        }
    }


    public function delArticle()
    {
        if (Request::isAjax()) {
            $result=Db::name('we_article')->where(['mid' => $this->mid, 'id' => input('fid')])->find();
            if (!empty($result)){

                Db::name('we_article')->where(['mid' => $this->mid, 'id' => input('fid')])->delete();

                ajaxMsg(1, '删除成功');
            }else{
                ajaxMsg(0, '没有删除的内容');
            }


        }
    }

    public function showArticle()
    {
        if (Request::isAjax()) {
            Db::name('we_article')->where(['mid' => $this->mid, 'id' => input('fid')])->update(['show'=>input('show')]);

        }
    }

    public function setTop()
    {
        if (Request::isAjax()) {
            Db::name('we_article')->where(['mid' => $this->mid, 'id' => input('fid')])->update(['top'=>input('show')]);

        }
    }
}